package org.argus.management.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.argus.common.core.enums.StatusEnum;
import org.argus.common.core.enums.UserType;
import org.argus.common.web.core.security.TokenManager;
import org.argus.common.web.core.snowflake.SnowflakeIdGenerator;
import org.argus.management.domain.Domain;
import org.argus.management.system.mapper.DomainMapper;
import org.argus.management.system.model.DomainSearchReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 刘文/Cliff Liu
 * @Date 2024/5/10
 */
@Service
@Slf4j
public class DomainService {

    @Autowired
    private DomainMapper domainMapper;

    @Autowired
    private TokenManager tokenManager;

    @Autowired
    private SnowflakeIdGenerator idGenerator;

    public List<Domain> getUserDomains() {
        String userType = tokenManager.getUserType();
        // 超级用户显示所有域
        if (UserType.SUPER.name().equals(userType)) {
            return domainMapper.getAllDomains();
        }
        Long userId = tokenManager.getUserId();
        return domainMapper.getUserDomains(userId);
    }

    public List<Domain> getAllDomains() {
        if (UserType.SUPER.name().equals(tokenManager.getUserType())) {
            return domainMapper.getAllDomains();
        }
        return null;
    }

    public List<Long> getUserDomainIds(Long userId) {
        return domainMapper.getUserDomains(userId).stream().map(Domain::getId).collect(Collectors.toList());
    }

    public Page<Domain> page(DomainSearchReq searchReq, Page page) {
        LambdaQueryWrapper<Domain> queryWrapper = Wrappers.lambdaQuery(Domain.class);
        queryWrapper.in(Domain::getStatus, Arrays.asList(StatusEnum.NORMAL, StatusEnum.INACTIVE));
        if (searchReq != null && StringUtils.isNotBlank(searchReq.getName())) {
            queryWrapper.like(Domain::getName, searchReq.getName());
        }
        if (searchReq != null && searchReq.getStatus() != null) {
            queryWrapper.eq(Domain::getStatus, searchReq.getStatus());
        }else {
            queryWrapper.in(Domain::getStatus, Arrays.asList(StatusEnum.NORMAL, StatusEnum.INACTIVE));
        }
        return domainMapper.selectPage(page, queryWrapper);
    }

    public void save(Domain domain) {
        int duplicateCount = domainMapper.getDuplicateCount(domain);
        if (duplicateCount > 0) {
            throw new RuntimeException("业务域名称或编码重复");
        }

        if (domain.getId() == null) {
            domain.setId(idGenerator.nextId());
            domainMapper.insert(domain);
            log.info("新增业务域：{}", domain.getName());
        }else{
            domainMapper.updateById(domain);
            log.info("更新业务域：{}", domain.getName());
        }
    }

    /**
     * 逻辑删除业务域
     * @param id
     */
    public void delete(Long id) {
        domainMapper.logicDelete(id);
    }
}
